<template>
  <div class="view">
    <OperateC
        :top-level="topLevel"
        :add="addMenu"
        :edit="editMenu"
        :remove="removeMenu"
        :add-show="this.$getButtonAuth('menu:add')"
        :remove-show="this.$getButtonAuth('menu:del:batch')"
    />
    <MenuTable
        :top-level="topLevel"
        :tableData="tableData"
        :edit-show="this.$getButtonAuth('menu:edit')"
        :del-show="this.$getButtonAuth('menu:del')"
        :add-child-show="this.$getButtonAuth('menu:add:child')"
    />
    <MenuDialog></MenuDialog>

  </div>
</template>

<script>
import MenuDialog from "@/components/dialog/MenuDialog";
import MenuTable from "@/components/table/MenuTable";
import OperateC from "@/components/OperateC";


import {deleteMenuByIds, getMenus} from "@/api/menu";
export default {
  name: "MenuView",
  components:{MenuTable, OperateC, MenuDialog},
  data() {
    return {
      topLevel: -1
    }
  },
  computed: {
    tableData: {
      get() {
        return this.$store.state.menu.tableData;
      },
      set(value) {
        this.$store.state.menu.tableData = value;
      }
    }
  },
  methods: {
    getMenus() {
      var params = {
        "current": this.$store.state.menu.currentPage,
        "size": this.$store.state.menu.pageSize
      };
      getMenus(params).then((res) => {
        this.$store.state.menu.tableData = res.data.data;
        this.$store.state.menu.total = res.data.total;
      })
    },
    addMenu() {
      var params = {
        dialogFormVisible: true,
        parentId: 0,
        parentName: '主目录',
        dialogTitle: '添加菜单'
      }
      this.$store.commit("menu/openDialogForm",params)
    },
    editMenu() {
      var selected = this.$store.state.menu.multipleSelection;
      if (selected.length < 1) {
        this.$message.warning("你还没有选中数据哦！");
        return;
      }
      if (selected.length > 1) {
        this.$message.warning("一次只能修改一条数据哦！")
        return;
      }
      this.$store.dispatch("menu/editMenu", selected[0]);
    },
    removeMenu() {
      var selected = this.$store.state.menu.multipleSelection;
      if (selected.length < 1) {
        this.$message.warning("请先选择要删除的数据哦！");
        return;
      }
      this.$confirm('确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMenuByIds(selected).then((res) => {
          this.$message.success(res.data.msg);
          // 刷新
          var params = {
            "current": this.$store.state.menu.currentPage,
            "size": this.$store.state.menu.pageSize
          };
          getMenus(params).then((res) => {
            this.$store.state.menu.tableData = res.data.data;
            this.$store.state.menu.total = res.data.total;
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
  mounted() {
    if (this.$getButtonAuth('menu:page')) {
      this.getMenus();
    }
  }
}
</script>

<style>

</style>
